﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace MINERP.COM
{
    public class WinHotKey
    {
        private bool key_Ctrl = false;
        private bool key_Shift = false;
        private bool key_Alt = false;
        private bool key_Windows = false;
        private Keys key_other;

        public WinHotKey()
        {

        }

        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool RegisterHotKey(
              IntPtr hWnd,
              int id,
              KeyModifiers fsModifiers,
              Keys vk
              );

        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool UnregisterHotKey(
              IntPtr hWnd,
              int id
              );

        [Flags()]
        public enum KeyModifiers
        {
            None = 0,
            Alt = 1,
            Control = 2,
            Shift = 4,
            Windows = 8
        }

        public void SetHotKey(IntPtr hWnd, bool bCtrl, bool bShift, bool bAlt, bool bWindows, Keys nowKey, int hotkeyId)
        {
            try
            {
                key_Alt = bAlt;
                key_Ctrl = bCtrl;
                key_Shift = bShift;
                key_Windows = bWindows;
                key_other = nowKey;

                WinHotKey.KeyModifiers modifier = WinHotKey.KeyModifiers.None;

                if (key_Ctrl)
                    modifier |= WinHotKey.KeyModifiers.Control;
                if (this.key_Alt)
                    modifier |= WinHotKey.KeyModifiers.Alt;
                if (this.key_Shift)
                    modifier |= WinHotKey.KeyModifiers.Shift;
                if (this.key_Windows)
                    modifier |= WinHotKey.KeyModifiers.Windows;

                RegisterHotKey(hWnd, hotkeyId, modifier, nowKey);
            }
            catch
            {
                // MessageBox.Show("快捷键定义错误！");
            }
        }
    }
}
